import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '../layout/Layout.vue'
import Login from '../views/Login.vue'

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/',
        component: Layout,
        redirect: '/service-management',
        children: [
            {
                path: 'service-management',
                name: 'ServiceManagement',
                component: () => import('../views/ServiceManagement.vue')
            },
            {
                path: 'message-query',
                name: 'MessageQuery',
                component: () => import('../views/MessageQuery.vue')
            },
            {
                path: 'service-message',
                name: 'ServiceMessage',
                component: () => import('../views/ServiceMessage.vue')
            },
            {
                path: 'message-settings',
                name: 'MessageSettings',
                component: () => import('../views/MessageSettings.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('imtoken')
    if (to.path !== '/login' && !token) {
        next('/login')
    } else {
        next()
    }
})

export default router 